
/* <<< COPYRIGHT START >>>
 * Copyright 2006-Present OxygenSoftwareLibrary.com
 * Licensed under the GNU Lesser General Public License.
 * http://www.gnu.org/licenses/lgpl.html
 * 
 * @author: Ugorji Dick-Nwoke
 * <<< COPYRIGHT END >>>
 */


package oxygen.tool.wlfacade;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.management.ObjectName;

import oxygen.tool.facade.FacadeBenignException;
import oxygen.tool.facade.FacadeContext;
import oxygen.tool.wlfacade.helpers.WLList;
import oxygen.util.OxyTable;

public class WLMBSTree implements WLTree {
  private ObjectName cmo;
  private ObjectName prevcmo;
  private String domain;
  private String mbsname;
  private Object mbs;
  private WLJMXHandler jmxhdlr;
  
  private WLFacadeContextHelper wlctx;
  
  public WLMBSTree(String _mbsname, Object _mbs, WLFacadeContextHelper _wlctx) throws Exception {
    wlctx = _wlctx;
    mbsname = _mbsname;
    mbs = _mbs;
    jmxhdlr = new WLJMXHandler();
    setDomain(wlctx.getConnection().getDomainName());
  }
  
  public WLJMXHandler getJMXHandler() throws Exception {
    return jmxhdlr;
  }
  
  public Object getMBS() throws Exception {
    return mbs;
  }
  
  public Object pointer() {
    return cmo;
  }
  
  public String getNodeStringRep(Object o) throws Exception {
    return jmxhdlr.getStringRep((ObjectName)o, mbs, true);
  }
  
  public String getDomain() throws Exception {
    return domain;
  }
  
  public void setDomain(String s) throws Exception {
    FacadeContext ctx = wlctx.getFacadeContext();
    if(!ctx.isFlagSet(WLFacadeContextHelper.CONNECTED)) {
      return;
    }
    if(domain != null && domain.equals(s)) {
      return;
    }
    List l = Arrays.asList(WLScriptHelper.getDomains(mbs));
    if(!l.contains(s)) {
      throw new FacadeBenignException("Domain does not exist");
    }
    domain = s;
    if(wlctx.getConnection().getDomainName().equals(domain)) {
      ctx.setFlag(WLFacadeContextHelper.PRIMARYDOMAIN);
    } else {
      ctx.clearFlag(WLFacadeContextHelper.PRIMARYDOMAIN);
    }
    cmo = null;
  }
  
  /**
   * can take dir like
   * Security:/realmDefaultAuthenticator
   * /realm
   * Server=crmserver
   */
  public Object go(String dir) throws Exception {
    WLScriptHelper.ensureConnected(wlctx);
    ObjectName cmo1 = cmo;
    ObjectName prevcmo1 = prevcmo;
    if("-".equals(dir)) {
      cmo1 = prevcmo1;
    } else {
      cmo1 = WLScriptHelper.getObjName(wlctx, dir);
    }
    setPointer(cmo1);
    return pointer();
  }

  public void setPointer(Object o) throws Exception {
    ObjectName cmo1 = (ObjectName)o;
    FacadeContext ctx = wlctx.getFacadeContext();
    if(cmo1 == null) {
      ctx.setFlag(WLFacadeContextHelper.DOMAINROOT);
    } else {
      ctx.clearFlag(WLFacadeContextHelper.DOMAINROOT);
      //set again, since switchDomain clears it ...
      setDomain(cmo1.getDomain());
    }
    
    prevcmo = cmo;
    cmo = cmo1;
  }
  
  public OxyTable find(String stringrep) throws Exception {
    return WLList.find(wlctx, stringrep);
  }
  
  public OxyTable list(Map matchMap) throws Exception {
    return WLList.list(wlctx, matchMap);
  }

  public OxyTable listatts(String regex) throws Exception {
    return WLList.listatts(wlctx, regex);
  }

  public OxyTable listops(String regex) throws Exception {
    return WLList.listops(wlctx, regex);
  }

  public OxyTable listroots() throws Exception {
    return WLList.listroots(wlctx);
  }

  public OxyTable listtypes(Map matchMap) throws Exception {
    return WLList.listtypes(wlctx, matchMap);
  }

  public OxyTable listdomains() throws Exception {
    return WLList.listdomains(wlctx);
  }
  
  public String getPrompt() throws Exception {
    FacadeContext ctx = wlctx.getFacadeContext();
    StringBuffer buf = new StringBuffer();
    buf.append("[wl:mbs:");
    
    if(ctx.isFlagSet(WLFacadeContextHelper.CONNECTED)) {
      //System.out.println("connected = true");
      //WLScriptConnection wlconn = getConnection();
      //Object o = wlconn.getServerName();
      //System.out.println(o);
      //System.out.println(getConnection().getServerName());
      //System.out.println("buf: " + buf);
      //buf.append(getConnection().getServerName())
      buf.append(mbsname).append(":")
        .append(wlctx.getConnection().getServerName())        
        .append("]").append(" ")
        .append(domain).append(":")
        .append(getNodeStringRep(cmo));
    } else {
      //System.out.println("connected = false");
      buf.append("Not Connected]");
    }
    
    buf.append(" > ");
    return buf.toString();
  }

}

